<?php

/**
 * User:ayou chow
 * Date: 2019-11-06
 * Time: 17:40
 */

namespace app\common\model;

use think\Model;

class UserModel extends Model
{
    // 账号类型
    const ACCOUNT_TYPE = [
        1 => '用户',
        2 => '测试用户',
        3 => '主播',
        // 4 => '公司主播',
        5 => '测试主播',
    ];

    public function profile()
    {
        return $this->hasOne(UserProfileModel::class, 'uid', 'id');
    }

    public function live()
    {
        return $this->hasOne(LiveModel::class, 'anchorid', 'id')->with('anchor');
    }

    public function roomAdmins()
    {
        return $this->hasOne(LiveRoomManagerModel::class, 'mgrid', 'id');
    }

    public function bannerdUser()
    {
        return $this->hasOne(LiveRoomBanneduserModel::class, 'uid', 'id');
    }

    public function anchorAuth()
    {
        return $this->hasOne(UserAuthModel::class, 'uid', 'id');
    }

    public function anchorBanned()
    {
        return $this->hasOne(AnchorBannedModel::class, 'anchor_id', 'id');
    }

    public function inLive()
    {
        return $this->hasOne(LiveModel::class, 'anchorid', 'id');
    }

    public function anchorLiveNotice()
    {
        return $this->hasOne(LiveNoticeModel::class, 'anchorid', 'id')->field("anchorid,content");
    }

    public function agent()
    {
        return $this->hasOne(AgentModel::class, 'uid', 'id');
    }

    public function orderInfo()
    {
        return $this->hasOne(OrderModel::class, 'uid', 'id');
    }

    public function guildManager()
    {
        return $this->hasOne(GuildManagerModel::class, 'id', 'guild_manager_id')->field('id,real_name');
    }

    public function guild()
    {
        return $this->hasOne(GuildModel::class, 'id', 'guild_id');
    }

    public function regsource()
    {
        return $this->hasOne(RegSourceModel::class, 'source_key', 'reg_source_id');
    }

    public function nobleRecord()
    {
        return $this->hasOne(NobleRecordModel::class, 'uid', 'id')->with(['noble']);
    }

    public function noble()
    {
        return $this->hasMany(NobleRecordModel::class, 'uid', 'id');
    }

    public function recommend()
    {
        return $this->hasOne(UserRecommendModel::class, 'user_id', 'id');
    }


    public function checkin()
    {
        return $this->hasOne(UserCheckinModel::class, 'uid', 'id');
    }

    public function gchat()
    {
        return $this->hasOne(GroupChatModel::class, 'anchor_id', 'id');
    }



    /**
     * 获取用户账号类型
     * 返回账号类型 1=用户，2=测试用户，3=主播，4=公司主播，5=测试主播
     * @param $is_anchor 0=用户，1=主播
     * @param $anchor_type 1=普通主播，2=公司主播，3=测试主播
     * @return int
     */
    public static function userAccountType($is_anchor = 0, $anchor_type = 1)
    {
        // 默认普通用户
        $account_type = 1;

        if ($is_anchor == 0) {
            $account_type = $anchor_type == 3 ? 2 : 1;
        } else {
            switch ($anchor_type) {
                case 1:
                    $account_type = 3;
                    break;
                case 2:
                    $account_type = 4;
                    break;
                case 3:
                    $account_type = 5;
                    break;
            }
        }
        return $account_type;
    }


    /**
     * 清理主播缓存
     * 主要是修改了is_anchor字段的都清理
     */
    public static function delAnchorCache()
    {
        redis_del("attentIds");
    }

    /**
     * 清理快速登录缓存
     * @param $uid
     */
    public static function delFastLoginCache($uid)
    {
        redis_del('USER:LAST:TOKEN:' . $uid);
    }

    public static function delUserCache($uid)
    {
        redis_del('USER:FULLINFO:' . $uid);
    }

    public static function getInfo($uid) {

        $userInfo = redis_get('USER:FULLINFO:' . $uid);
        if (!$userInfo) {
            if (!$user = UserModel::where('id', $uid)->field('*')->lock()->find()) {
                return [];
            }
            return $user->toArray();
        }
        if (is_string($userInfo) && $userInfo !== '') {
            return json_decode($userInfo, true);
        }
        return [];
    }
}
